package com.example.day5;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.RatingBar;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;

import java.util.Timer;
import java.util.TimerTask;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    ProgressBar pro2, pro3, pro;
    TextView text1, text2, text3;
    SeekBar seekBar1, seekBar3;
    RatingBar ratingBar;
    Button toast1, toast2, toast3, toast4, toast5, toast6, toast7, toast8;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        pro2 = findViewById(R.id.progressBar2);
        pro3 = findViewById(R.id.progressBar3);
        pro = findViewById(R.id.progressBar);
        text1 = findViewById(R.id.text1);
        text2 = findViewById(R.id.text2);
        text3 = findViewById(R.id.text3);
        seekBar1 = findViewById(R.id.seekBar);
        seekBar3 = findViewById(R.id.seekBar3);
        ratingBar = findViewById(R.id.ratingBar);
        toast2 = findViewById(R.id.toast2);
        toast3 = findViewById(R.id.toast3);
        toast4 = findViewById(R.id.toast4);
        toast5 = findViewById(R.id.toast5);
        toast6 = findViewById(R.id.toast6);
        toast7 = findViewById(R.id.toast7);
        toast8 = findViewById(R.id.toast8);
        toast1 = findViewById(R.id.toast1);
        toast1.setOnClickListener(this);
        toast2.setOnClickListener(this);
        toast3.setOnClickListener(this);
        toast4.setOnClickListener(this);
        toast5.setOnClickListener(this);
        toast6.setOnClickListener(this);
        toast7.setOnClickListener(this);
        toast8.setOnClickListener(this);


        pro2.setVisibility(View.GONE);
        new MyThread().start();
        ratingBar.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
            @Override
            public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
                text1.setText(ratingBar.getProgress() + "");
                text2.setText(rating + "");
                Toast.makeText(MainActivity.this, "sdasd", Toast.LENGTH_LONG);
            }
        });
        seekBar3.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                text1.setText(seekBar.getProgress() + "");
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
                text2.setText(seekBar.getProgress() + "");
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                text3.setText(seekBar.getProgress() + "");
            }
        });

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.toast1:
                Toast.makeText(this, "toast1默认toast测试", Toast.LENGTH_SHORT).show();
                break;
            case R.id.toast2:
                Toast toast = Toast.makeText(this, "toast2自定义位置toast测试", Toast.LENGTH_SHORT);
                Log.e("###Gravity.LEFT",Gravity.LEFT+"");
                Log.e("###Gravity.Top",Gravity.TOP+"");
                Log.e("###Gravity.Top+LEFT",(Gravity.LEFT|Gravity.TOP)+"");
                toast.setGravity(Gravity.LEFT|Gravity.TOP, 0, 0);
                toast.show();
                break;
            case R.id.toast3:
                Toast toast3 = new Toast(MainActivity.this);
                View toastView = getLayoutInflater().inflate(R.layout.toast_show, null);
                toast3.setView(toastView);
                toast3.setDuration(Toast.LENGTH_LONG);
                toast3.show();

                break;
            case R.id.toast4:
                CustomTimeToast("自定义显示时间的toast");
            break;
            case R.id.toast5:
                break;
            case R.id.toast6:
                break;
            case R.id.toast7:
                break;
            case R.id.toast8:
                break;
        }
    }
    private void CustomTimeToast (String show) {
        final Toast toast = Toast.makeText(this, show,
                Toast.LENGTH_LONG);
        final Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                toast.show();
            }
        }, 0, 1000);// 3000表示点击按钮之后，Toast延迟3000ms后显示
        new Timer().schedule(new TimerTask() {
            @Override
            public void run() {
                toast.cancel();
                timer.cancel();
            }
        }, 20000);// 5000表示Toast显示时间为5秒

    }
    public class MyThread extends Thread {
        //继承Thread类，并改写其run方法
        private final static String TAG = "My Thread ===> ";

        public void run() {
            for (int i = 0; i <= 100; i += 10) {
                pro.setProgress(i);
                try {
                    sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }


}
